﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;
namespace Ebada.Modules.Document
{
    /// <summary> 
    /// 单证类型编辑
    /// </summary>
    public partial class frmDocumentsEdit : FormBase, IPopupFormEdit {
      
        public IList<DocumentImage> _list=null;
        FuncLib fun = new FuncLib();
        private string FieldName = "";
        private byte[] img1=null;
        private byte[] img2=null;
        private byte[] img3=null;
        public bool bdflag = false;
        public int bttype = 0;
        public frmDocumentsEdit() {
            InitializeComponent();
            IList<DocumentPosition>  dp_list = MainHelper.PlatformSqlMap.GetList<DocumentPosition>("");
            SetComboBoxData(txtDocumentPositionID, "PositionName", "DocumentPositionID", "请选择", "单证位置", dp_list);
            IList<mOrg> org_list = MainHelper.PlatformSqlMap.GetList<mOrg>("");
            lookOrg_ID.Properties.DataSource = org_list;
            lookUpEdit1.Properties.DataSource = org_list;
        }
        void dataBind() {

            this.txtDocumentPositionID.DataBindings.Add("EditValue", rowData, "DocumentPositionID");
            this.lookUpEdit1.DataBindings.Add("EditValue", rowData, "OrgID");
            this.txtDocState.DataBindings.Add("EditValue", rowData, "DocState");
            this.txtDocCode.DataBindings.Add("EditValue", rowData, "DocCode");
            this.txtAct_Name.DataBindings.Add("EditValue", rowData, "Act_Name");
            this.txtAct_Code.DataBindings.Add("EditValue", rowData, "Act_Code");
            this.txtBusinessCode.DataBindings.Add("EditValue", rowData, "BusinessCode");
            this.dateAct_Date.DataBindings.Add("EditValue", rowData, "Act_Date");
            this.dateSubmitDateLimit.DataBindings.Add("EditValue", rowData, "SubmitDateLimit");
            this.dateSubmitDate.DataBindings.Add("EditValue", rowData, "SubmitDate");
            this.dateValidityDate.DataBindings.Add("EditValue", rowData, "ValidityDate");
            this.dateRemindDate.DataBindings.Add("EditValue", rowData, "RemindDate");
            this.txtCreateUserName.DataBindings.Add("EditValue", rowData, "CreateUserName");
            this.dateCreatedDate.DataBindings.Add("EditValue", rowData, "CreatedDate");
            this.txtLastEditUserName.DataBindings.Add("EditValue", rowData, "LastEditUserName");
            this.dateLastEditTime.DataBindings.Add("EditValue", rowData, "LastEditTime");
            this.comCheckResult.DataBindings.Add("EditValue", rowData, "CheckResult");
            this.memoEdit1.DataBindings.Add("EditValue", rowData, "CheckRemark");
            this.txtCheckUserName.DataBindings.Add("EditValue", rowData, "CheckUserName");
            this.dateCheckedDate.DataBindings.Add("EditValue", rowData, "CheckedDate");
            this.lookOrg_ID.DataBindings.Add("EditValue", rowData, "Org_ID");
            this.dateScrapDate.DataBindings.Add("EditValue", rowData, "ScrapDate");
        }
        #region IPopupFormEdit Members
        private Documents rowData = null;

        public object RowData {
            get {
                return rowData;
            }
            set {
                if (value == null)
                {
                    return;
                }
             
                if (rowData == null) {
                    this.rowData = value as Documents;
                    getDocumentTypeInfo();
                    dataBind();
                } else {
                    ConvertHelper.CopyTo<Documents>(value as Documents, rowData);
                    getDocumentTypeInfo();
                }
                LoadGrid();
                if (rowData.DocState == 0 && rowData.DocCode == "")
                {
                    simpleButton1.Visible = false;
                    btnOK.Visible = true;
                    txtCheckUserName.Visible = false;
                    dateCheckedDate.Visible = false;
                    labelControl8.Visible = false;
                    labelControl9.Visible = false;
              
                    btnOK.Text = "增加";
                }
                //修改
                if (rowData.DocState == 0 && rowData.DocCode != "" && bttype==0)
                {
                    simpleButton1.Visible = false;
                    btnOK.Visible = true;
                    txtCheckUserName.Visible = false;
                    dateCheckedDate.Visible = false;
                    labelControl8.Visible = false;
                    labelControl9.Visible = false;
              
                    btnOK.Text = "修改";
                }

                if (rowData.DocState == 1 && !bdflag)
                {
                    simpleButton1.Visible = false;
                    btnOK.Visible = true;
                    txtCheckUserName.Visible = true;
                    dateCheckedDate.Visible = true;
                    labelControl8.Visible = true;
                    labelControl9.Visible = true;
               
                    btnOK.Text = "修改";
                }
                if (rowData.DocState == 1 && bdflag)
                {
                    simpleButton1.Visible = false;
                    txtCheckUserName.Visible = true;
                    dateCheckedDate.Visible = true;
                    labelControl8.Visible = true;
                    labelControl9.Visible = true;
                
                    btnOK.Visible = true;
                    btnOK.Text = "补单";
                }
                //提交
                if (rowData.DocState == 0 && rowData.DocCode != "" && bttype == 1)
                {
                    simpleButton1.Visible = true;
                    btnOK.Visible = false;
                    txtCheckUserName.Visible = false;
                    dateCheckedDate.Visible = false;
                    labelControl8.Visible = false;
                    labelControl9.Visible = false;
                    txtCheckUserName.Enabled = false;
                    dateCheckedDate.Enabled = false;
                    dateValidityDate.Enabled=false;
                    dateSubmitDateLimit.Enabled=false;
                    dateRemindDate.Enabled = false;
                 
                    rowData.SubmitDate = DateTime.Now;
                    rowData.Act_Date = DateTime.Now;
                }
                //审核
                if (rowData.DocState == 1  && bttype == 2)
                {
                    rowData.CheckedDate = DateTime.Now;
                    rowData.CheckUserName = MainHelper.UserBaseX.Empolyee_Name;
                    txtCheckUserName.Visible = true;
                    dateCheckedDate.Visible = true;
                    labelControl8.Visible = true;
                    labelControl9.Visible = true;
                    txtCheckUserName.Enabled = false;
                    dateCheckedDate.Enabled = false;
                    simpleButton1.Visible = false;
                    dateValidityDate.Enabled = false;
                    dateSubmitDateLimit.Enabled = false;
                    dateRemindDate.Enabled = false;
                    rowData.SubmitDate = DateTime.Now;
                    rowData.Act_Date = DateTime.Now;
                    
                    btnOK.Visible = true;
                    btnOK.Text = "审核";
                }
              
                if (btnOK.Text == "审核")
                {
                    labelck.Visible = true;
                    comCheckResult.Visible = true;
                    memoEdit1.Visible = true;
                    labelControl13.Visible = true;
                }
                else
                {
                    labelck.Visible = false;
                    comCheckResult.Visible = false;
                    memoEdit1.Visible = false;
                    labelControl13.Visible = false;
                }
            }
        }

        #endregion

        private void InitComboBoxData(string cityCode) {
           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        /// <param name="nullTest"></param>
        /// <param name="cnStr"></param>
        /// <param name="post"></param>
        public void SetComboBoxData(DevExpress.XtraEditors.LookUpEdit comboBox, string displayMember, string valueMember, string nullTest, string cnStr, IList<DocumentPosition> post)
        {
            comboBox.Properties.Columns.Clear();
            comboBox.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            comboBox.Properties.DataSource = post;
            comboBox.Properties.DisplayMember = displayMember;
            comboBox.Properties.ValueMember = valueMember;
            comboBox.Properties.NullText = nullTest;
            comboBox.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(valueMember, "DocumentPositionID", 20, DevExpress.Utils.FormatType.None, "", false, DevExpress.Utils.HorzAlignment.Default),
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(displayMember, cnStr)});
        }

        private void groupControlOrg_Paint(object sender, PaintEventArgs e)
        {

        }
        public void LoadGrid()
        {
            _list = MainHelper.GetSqlMap<DocumentImage>().GetListByWhere<DocumentImage>("  where DocumentID='" + rowData.DocumentID + "'");
            if (_list.Count > 0)
            {
                img1 = (byte[])_list[0].DocImage.Clone();
               // img2 = (byte[])_list[0].DocImage2.Clone();
                img3 = (byte[])_list[0].DocFile.Clone();
                _list[0].DocImage = fun.GetByte(_list[0].DocImage);
               // _list[0].DocImage2 = fun.GetByte(_list[0].DocImage2);
                _list[0].DocFile = fun.GetByte(_list[0].DocFile);
            }
            gridControl1.DataSource = _list;
        }
        public void getDocumentTypeInfo()
        {
            if (rowData.DocCode == "")
            {
                DocumentType dtype = MainHelper.GetSqlMap<DocumentType>().GetOneByKey<DocumentType>(rowData.DocumentTypeID);
                rowData.Act_Name = dtype.Act_Name;
                rowData.Act_Code = dtype.Act_Code;
                rowData.CreateUserName = MainHelper.UserBaseX.Empolyee_Name;
                rowData.CreatedDate = DateTime.Now;
                rowData.DocumentTypeName = dtype.DocTypeName;
                rowData.SubmitDate = DateTime.Now;
                rowData.Act_Date = DateTime.Now;
                rowData.LastEditTime = DateTime.Now;
                rowData.SubmitDateLimit = DateTime.Now;
                if (dtype != null)
                {
                    rowData.ValidityDate = rowData.SubmitDate.AddDays(Convert.ToDouble(dtype.ValidityDate));
                    rowData.RemindDate = rowData.ValidityDate.AddDays(Convert.ToDouble(-dtype.RemindDate));
                    rowData.ScrapDate = rowData.ValidityDate;
                }
            }
            if (rowData.DocCode!="")
            {
                rowData.LastEditUserName = MainHelper.UserBaseX.Empolyee_Name;
                rowData.LastEditTime = DateTime.Now;
            }
            //rowData.ValidityDate = DateTime.Now;
            //rowData.SubmitDateLimit = DateTime.Now;
            //rowData.RemindDate = DateTime.Now;
        }
        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_list.Count > 0)
            {
                MessageBox.Show("已经添加过单证附件，不能重复添加。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
                
            }
            frmImageSel f = new frmImageSel();
            f.ParentID = rowData.DocumentID;
            if (f.ShowDialog() == DialogResult.OK)
            {
                LoadGrid();
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_list.Count < 1) return;
            if (MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information)==DialogResult.Yes)
            {
                MainHelper.GetSqlMap<DocumentImage>().DeleteByKey<DocumentImage>(_list[0].DocumentImageID);
                LoadGrid();
            }
        }

        private void 浏览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (FieldName == "") return;
            if (_list.Count < 1) return;
            if (FieldName == "DocImage")
            {
                fun.ViewImage(img1,"图片信息");
            }
          
            if (FieldName == "DocFile")
            {
                fun.ViewImage(img3, "电子文档");
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要提交此单证吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                
                rowData.DocState = 1;
                rowData.DocStateName = "提交";
                rowData.SubmitDate = DateTime.Now;
                DocumentType dtype = MainHelper.GetSqlMap<DocumentType>().GetOneByKey<DocumentType>(rowData.DocumentTypeID);
                rowData.DocumentTypeName = dtype.DocTypeName;
                if(dtype!=null){
                    rowData.ValidityDate = rowData.SubmitDate.AddDays( Convert.ToDouble( dtype.ValidityDate));
                    rowData.RemindDate = rowData.ValidityDate.AddDays(Convert.ToDouble(-dtype.RemindDate));
                    rowData.ScrapDate = rowData.ValidityDate;
                }
               
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void gridView2_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            FieldName= e.FocusedColumn.FieldName;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtDocCode.Text == "")
            {
                MessageBox.Show("单证编号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtDocCode.Focus();
                return;
            }
            if(rowData.DocState == 0){
                rowData.DocStateName = "未提交";
            }
            if (btnOK.Text == "审核")
            {
                if (comCheckResult.Text == "")
                {
                    MessageBox.Show("审查结果不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    comCheckResult.Focus();
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtDocumentPositionID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString() == "2")
            {
                frmDocumentPositionSeletr f = new frmDocumentPositionSeletr();
                if (f.ShowDialog() == DialogResult.OK)
                {
                    if (f.D_Position != null)
                    {
                        txtDocumentPositionID.EditValue = f.D_Position.DocumentPositionID;
                    }
                }
            }
        }

        private void lookOrg_ID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString() == "2")
            {

                Ebada.Modules.Vehicle.frmOrgSelector2 f = new Ebada.Modules.Vehicle.frmOrgSelector2();
                f.IntionDataAll();
                if (f.ShowDialog() == DialogResult.OK)
                {
                    if (f.SelectObj != null)
                    {
                        lookOrg_ID.EditValue = f.SelectObj.Org_ID;
                    }
                }
            }
        }

        private void dateSubmitDate_EditValueChanged(object sender, EventArgs e)
        {
            DocumentType dtype = MainHelper.GetSqlMap<DocumentType>().GetOneByKey<DocumentType>(rowData.DocumentTypeID);
            rowData.SubmitDate = dateSubmitDate.DateTime;
            if (dtype != null)
            {
                rowData.ValidityDate = rowData.SubmitDate.AddDays(Convert.ToDouble(dtype.ValidityDate));
                rowData.RemindDate = rowData.ValidityDate.AddDays(Convert.ToDouble(-dtype.RemindDate));
                rowData.ScrapDate = rowData.ValidityDate;
            }
            dateValidityDate.DateTime = rowData.ValidityDate;
            dateRemindDate.DateTime = rowData.RemindDate;
            dateScrapDate.DateTime = rowData.ScrapDate;
        }

        private void lookUpEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString() == "2")
            {

                Ebada.Modules.Vehicle.frmOrgSelector2 f = new Ebada.Modules.Vehicle.frmOrgSelector2();
                f.IntionDataAll();
                if (f.ShowDialog() == DialogResult.OK)
                {
                    if (f.SelectObj != null)
                    {
                        lookUpEdit1.EditValue = f.SelectObj.Org_ID;
                        rowData.OrgName = f.SelectObj.cnName;
                    }
                }
            }
        }

     
    }
}